<?php
namespace api\jeu\perso\caracteristique;
use api\jeu\perso as nsPerso;

if (!defined('GAME'))
{
	exit;
}

abstract class baseCaracteristiqueNumerique extends baseCaracteristique implements iCaracteristiqueNumerique {

	private $_value;
	private $_max;
	private $_value_modifier;
	private $_max_modifier;
	private $_allowNegative;
	private $_allowOverflow;
	
	protected $_events = array('update','max','over','zero','negatif');
	
	function __construct($baseVal = 0, $baseMax = null, $baseNegative = false, $baseOverflow = false) {
		parent::__construct();
		$this->_value = $baseVal;
		$this->_max = $baseMax;
		$this->_allowNegative = $baseNegative;
		$this->_allowOverflow = $baseOverflow;
	}
	
	public function refreshBuff()
	{
		foreach ($this->_buffs as $buff)
		{
			if($buff->getSource() == "max" && $this->_max != null) {
				$modifier = "_max_modifier";
				$source = "_max";
			} else {
				$modifier = "_value_modifier";
				$source = "_value";
			}
			
			$this->$modifier = $buff->apply($this->$modifier, $this->$source);
		}
	}	
	
	final function getValue() {
		return $this->_value;
	}
	
	final function setValue($val) {
		$this->_value = $val;
	}
	
	final function getMax() {
		return $this->_max;
	}
	
	final function setMax($val = null) {
		$this->_max = $val;
	}
	
	final function getModifier() {
		return $this->_modifier;
	}
	
	final function setModifier($val = null) {
		$this->_modifier = $val;
	}	

	final function getNegative() {
		return $this->_allowNegative;
	}
	
	final function setNegative($val = false) {
		$this->_allowNegative = $val;
	}

	final function getOverflow() {
		return $this->_allowOverflow;
	}
	
	final function setOverflox($val = false) {
		$this->_allowOverflow = $val;
	}

	final function changeValue($val)
	{
		if(is_numeric($val))
		{
			$actu = $this->_value;
			
			$this->_value = $val;
			
			if(!$this->_allowNegative && $this->_value < 0) {
				$this->_value = 0;
			}		
			
			if(!is_null($this->_max)) {
				if(!$this->_allowOverflow && $this->_value > $this->_max) {
					$this->_value = $this->_max;
				}	

				if($this->_value > $this->_max) {
					$this->onOver();
				} elseif ($this->_value == $this->_max) {
					$this->onMax();
				}
			}

			if($this->_value < 0) {
				$this->onNegative();
			} elseif ($this->_value == 0) {
				$this->onZero();
			}

			if($actu != $this->_value) {
				$this->onChange();
			}
			
			$this->onUpdate();
		}	
	}
	
	final function decremente($val = 1) {
		if(is_numeric($val))
			$this->changeValue(0-abs($val));
	}
	final function incremente($val = 1) {
		if(is_numeric($val))
			$this->changeValue(abs($val));
	}
	
	private function onChange()
	{
		$this->notify('change');
	}
	
	private function onUpdate() 
	{
		$this->notify('update');	
	}
	
	private function onMax()
	{
		$this->notify('max');	
	}	
	
	private function onOver()
	{
		$this->notify('over');	
	}	
	
	private function onZero()
	{
		$this->notify('zero');	
	}	
	
	private function onNegative()
	{
		$this->notify('negatif');
	}

	public function isMax()
	{
		return ($this->_value === $this->_max) ? true : false;
	}
	
	public function isOver()
	{
		return ($this->_value > $this->_max) ? true : false;
	}
	
	public function isZero()
	{
		return ($this->_value == 0) ? true : false;
	}
	
	public function isNegative()
	{
		return ($this->_value < 0) ? true : false;
	}
	
}

?>